package com.sanjiahantang.youjiarole.module.me.require.views.impl;

import android.app.Dialog;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sanjiahantang.uilibrary.adapter.listview.BaseAdapterHelper;
import com.sanjiahantang.uilibrary.adapter.listview.QuickAdapter;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.base.BaseMvpActivity;
import com.sanjiahantang.youjiarole.base.BaseView;
import com.sanjiahantang.youjiarole.config.ConfigValue;
import com.sanjiahantang.youjiarole.module.invoice.views.InvoiceView;
import com.sanjiahantang.youjiarole.module.me.address.beans.AddressBean;
import com.sanjiahantang.youjiarole.module.me.address.views.AddressView;
import com.sanjiahantang.youjiarole.module.me.order.beans.RequireOrderBean;
import com.sanjiahantang.youjiarole.module.me.order.beans.Service3PartBean;
import com.sanjiahantang.youjiarole.module.me.order.beans.ServiceListBean;
import com.sanjiahantang.youjiarole.module.me.order.presenters.OrderPresenter;
import com.sanjiahantang.youjiarole.module.me.order.views.OrderView;

/**
 * Created by zy on 2017/10/24.
 */

public class RequireOrderDetailActivity extends BaseMvpActivity<OrderView, OrderPresenter> implements OrderView, View.OnClickListener {

    private View panelCreate,panelPay,panelEvaluate,rlInvoiceInfo;
    private TextView txtState;//状态文字
    private ImageView ivState;//状态图标
    private TextView txtOrderNum;//订单号
    private TextView txtCreateTime;//创建时间
    private ListView goodListView;//列表
    private TextView txtGoodsCount;//数量
    private TextView txtAmount;//价格
    private EditText etConsumerMessage;//留言
    private TextView txtTotal;//合计
    private TextView btnPayForStateUnPay;//应付金额
    private TextView title;
    private TextView txtSubmit;

    private boolean isRefreshing = false;
    private int type;
    private Service3PartBean.Data.OrderServiceRaDtoListBean responseData;


    @Override
    public void showProgress() {
        super.showProgress($this);
    }

    @Override
    public OrderPresenter initPresenter() {
        return new OrderPresenter();
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_order_info);
        //  presenter.bindList($this,listView);
        title = (TextView) findViewById(R.id.title);
        title.setText("订单详情");
        findViewById(R.id.fl_Left).setOnClickListener(this);

        panelCreate = findViewById(R.id.panelCreate);
        panelPay = findViewById(R.id.panelPay);
        panelEvaluate = findViewById(R.id.panelEvaluate);

        txtState = (TextView) findViewById(R.id.txtState);
        ivState = (ImageView) findViewById(R.id.ivState);
        txtOrderNum = (TextView) findViewById(R.id.txtOrderNum);
        txtCreateTime = (TextView) findViewById(R.id.txtCreateTime);
        goodListView = (ListView) findViewById(R.id.goodListView);
        txtGoodsCount = (TextView) findViewById(R.id.txtGoodsCount);
        txtAmount = (TextView) findViewById(R.id.txtAmount);
        etConsumerMessage = (EditText) findViewById(R.id.etConsumerMessage);
        txtTotal = (TextView) findViewById(R.id.txtTotal);
        btnPayForStateUnPay = (TextView) findViewById(R.id.btnPayForStateUnPay);
        txtSubmit = (TextView) findViewById(R.id.txtSubmit);
        txtSubmit.setOnClickListener(this);
        findViewById(R.id.btnEvaluate).setOnClickListener(this);

        findViewById(R.id.panelInvoice).setVisibility(View.GONE);
    }

    @Override
    public void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        loadData();
    }

    private void loadData(){
        RequireOrderBean.Data data = (RequireOrderBean.Data) getIntent().getSerializableExtra(KEY_PARAM);
        if (data.getOrderCode() != null){
            presenter.createOrderForDesign($this, data.getOrderCode(), new BaseView.OnLoadListener() {
                @Override
                public void onLoad(Object content) {
                    responseData = (Service3PartBean.Data.OrderServiceRaDtoListBean) content;
                    String statusName = responseData.getStatusName();
                    int resId = R.drawable.i_unpay_head;
                    switch (responseData.getStatusId()){
                        case OrderView.TYPE_UN_PAY:
                            statusName = "待付款";
                            resId = R.drawable.i_unpay_head;
                            panelPay.setVisibility(View.GONE);
                            panelCreate.setVisibility(View.GONE);
                            panelEvaluate.setVisibility(View.GONE);
                            break;
                        case OrderView.TYPE_PART_PAY:
                            resId = R.drawable.i_unpay_head;
                            panelPay.setVisibility(View.GONE);
                            panelCreate.setVisibility(View.GONE);
                            panelEvaluate.setVisibility(View.GONE);

                            txtTotal.setText(String.valueOf(responseData.getPayResult()));
                            txtSubmit.setText("去付款");
                            break;
                        case OrderView.TYPE_PAYED:
                            resId = R.drawable.i_payed_head;
                            panelPay.setVisibility(View.GONE);
                            panelCreate.setVisibility(View.GONE);
                            if (responseData.getServiceType() == 3 && responseData.getServiceRecordList().get(0).getPlanOrderState() == 1){
                                panelEvaluate.setVisibility(View.GONE);
                            }else{
                                panelEvaluate.setVisibility(View.GONE);
                            }

                            break;
                        case OrderView.TYPE_COMPLETE:
                            statusName = "已完成";
                            resId = R.drawable.i_payed_head;
                            panelEvaluate.setVisibility(View.GONE);
                            panelPay.setVisibility(View.GONE);
                            panelCreate.setVisibility(View.GONE);
                            break;
                        case OrderView.TYPE_CLOSE:
                            resId = R.drawable.i_unpay_head;
                            panelPay.setVisibility(View.GONE);
                            panelCreate.setVisibility(View.GONE);
                            panelEvaluate.setVisibility(View.GONE);
                            break;
                        case OrderView.TYPE_EVALUATED:
                            statusName = "已完成";
                            resId = R.drawable.i_unpay_head;
                            panelPay.setVisibility(View.GONE);
                            panelCreate.setVisibility(View.GONE);
                            panelEvaluate.setVisibility(View.GONE);
                            break;
                        default:
                            resId = R.drawable.i_unpay_head;
                            panelPay.setVisibility(View.GONE);
                            panelCreate.setVisibility(View.GONE);
                            panelEvaluate.setVisibility(View.GONE);
                            break;
                    }
                    txtState.setText(statusName);
                    ivState.setImageResource(resId);
                    txtOrderNum.setText(responseData.getOrderCode());
                    if (responseData.getCreateTime() != null){
                        txtCreateTime.setText(responseData.getCreateTime().substring(0,"yyyy-MM-dd".length()));
                    }

                    goodListView.setAdapter(new QuickAdapter<Service3PartBean.Data.OrderServiceRaDtoListBean.ServiceRecordListBean>($this,R.layout.item_serviceorderitem,responseData.getServiceRecordList()) {
                        @Override
                        protected void convert(BaseAdapterHelper helper, Service3PartBean.Data.OrderServiceRaDtoListBean.ServiceRecordListBean item) {
                            helper.setText(R.id.sg_name,item.getServiceName());
                            helper.setText(R.id.sg_price,String.valueOf(item.getPrice() * item.getCount()));
//                        helper.setText(R.id.sg_count,String.valueOf(item.getCount()));
                        }
                    });

                    txtAmount.setText(String.valueOf(responseData.getPayMoney()));
                    txtGoodsCount.setText(String.valueOf(responseData.getServiceRecordList().size()));
                    btnPayForStateUnPay.setText(String.format("本次应付%.2f元",(float)responseData.getPayResult()));
                }
            });

            btnPayForStateUnPay.setOnClickListener(this);
            findViewById(R.id.btnCancel).setOnClickListener(this);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == INTENT_INVOICE && resultCode == RESULT_OK){
            if(data != null){
                presenter.setInvoice(data.getStringExtra(KEY_TITLE),data.getStringExtra(InvoiceView.KEY_TAX_NO));
            }
        }
        if (requestCode == INTENT_ADDRESS && resultCode == RESULT_OK){
            if(data != null){
                AddressBean.Data address = new AddressBean.Data();
                address.setCustomerName(data.getStringExtra(AddressView.KEY_NAME));
                address.setFullAddress(data.getStringExtra(AddressView.KEY_ADDRESS));
                address.setPhone(data.getStringExtra(AddressView.KEY_PHONE));
                presenter.setAddress(address);
            }
        }

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fl_Left:
                finish();
                break;
        }
    }
}
